/*package com.yc.jpyy.facecheck.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import okhttp3.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.yc.jpyy.base.bean.Client;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.dao.TeacherDao;
import com.yc.jpyy.mobile.service.BizService;
import com.yc.jpyy.mobile.service.TeacherService;

@Controller
@RequestMapping(value = "/facecheck")
public class LoginAndOutController extends BaseController {
	
	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

	private static Logger logger = LoggerFactory.getLogger(LoginAndOutController.class);
	
	@Autowired
	private  BizService bizService;
	
	@Autowired
	private  BizDao bizDao;
	
	@Autowired
	private TeacherService teacherService;
	
	@Autowired
	private  TeacherDao teacherDao;
	
	public  static String uri = Config.getCompareQRCodeUrl();
	WebSocketContainer container = ContainerProvider.getWebSocketContainer();
	Client client = new Client();
    Session session = null;
	*//**
	 * 登录/登出确认通知
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 *//*
	@SuppressWarnings("finally")
	@RequestMapping(value = "/SigninConfirm", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String signinConfirm(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练/学员全国编号
			String ObjectNum = request.getParameter("ObjectNum");
			// 终端编号
			String Devnum = request.getParameter("Devnum");
			// 请求对象类型
			String Type = request.getParameter("Type");
			// 事件类型
			String EventType = request.getParameter("EventType");
			//身份证UID
			String UID = request.getParameter("UID");
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("USERID", 1);
			map.put("COMMAND_ID", 3);
			map.put("commandId", "904508");
			map.put("commandType", "904");
			map.put("commandCode", "8900");
			Date date = new Date();
			String timestamp = String.valueOf(date.getTime()); 
			map.put("requestID", timestamp);
			map.put("commandBody", "");

			if(ObjectNum != null && !ObjectNum.isEmpty()
					&& Type != null && !Type.isEmpty()
					&& EventType != null && !EventType.isEmpty()){
				//service层处理登录登出业务
				//teacherService.signinConfirm(UID,Type,ObjectNum,EventType,Devnum,map,response);	
				String remindString ="身份证标识已存在，请联系客服！";
				if(null != UID && !UID.isEmpty()){
					//type=1教练；type=2学员
					if(Integer.parseInt(Type) == 1){
						//防止第一个人刷身份证刷出二维码，却是另外一个人扫了二维码
						//根据ObjectNum获取教练的UId
						String coachUID = teacherDao.getUIDForFaceCheck(ObjectNum);
						//如果教练的UId存在且不为空(第一次登陆才传身份证号，这时库里教练的身份证是不存在的，如果存在则提示身份标识已存在)
						if(null != coachUID && !coachUID.equals("")){
							 response.setErrorcode(1);
							 response.setMessage(remindString);
						}else{
							 //查询该UID是否存在（除了ObjectNum对应的记录，其他记录里是否存在该UID）
						     int uIdCount = teacherDao.isExistUIDForFaceCheck(UID);
						     //如果存在则提示身份标识已存在
							 if(uIdCount > 0){
								 response.setErrorcode(1);
								 response.setMessage(remindString);
							 }else{
								 //第一次登陆，如果库里不存在，则把身份证号更新进去（以后登录不会再传身份证号）
								 teacherDao.updateUIDForFaceCheck(UID, ObjectNum); 
								 //验证设备编号(VehicleId和gpsId)并访问二维码比对接口
								 checkDevNum(response,Devnum,ObjectNum,Type,EventType,map);
							 }
						}
					}else if(Integer.parseInt(Type)==2){
						//学员同教练原理一样
						String stuUID = bizDao.getUIDForFaceCheck(ObjectNum);
						if(null != stuUID && !stuUID.equals("")){
							 response.setErrorcode(1);
							 response.setMessage(remindString);
						}else{
						     int uIdCount = bizDao.isExistUIDForFaceCheck(UID);
							 if(uIdCount > 0){
								 response.setErrorcode(1);
								 response.setMessage(remindString);
							 }else{
								 bizDao.updateUIDForFaceCheck(UID, ObjectNum);
								 //验证设备编号并访问二维码比对接口
								 checkDevNum(response,Devnum,ObjectNum,Type,EventType,map);
							 }
						}
					}
				}else{
					//不是第一次登录（扫码），则直接验证设备编号并访问二维码比对接口
					checkDevNum(response,Devnum,ObjectNum,Type,EventType,map);
				}
			}else{
				response.setErrorcode(1);
				response.setMessage("必填字段为空...");
			}		    
		} catch (Exception e) {
			logger.info("signinConfirm："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("访问失败，请稍候重试！");
		} finally {
			return response.toJSON();
		}
	}
	
	   //验证设备编号(VehicleId和gpsId)并访问二维码比对接口
		public void checkDevNum(RestResponse response, String Devnum, String ObjectNum, String Type, String EventType, Map<String, Object> map)throws Exception{
			//根据设备编号查VehicleId
			Integer VehicleId = bizService.getVehicleIdForFaceCheck(Devnum);
			if(null != VehicleId && VehicleId != 0){
				//根据设备编号查GpsId
				Integer gpsId = bizService.getGpsIdForFaceCheck(VehicleId);
				if( null != gpsId){
					map.put("VEHICLEID", gpsId);
					map.put("commandArgs", "13,8D05," + ObjectNum + "," + Type + "," + EventType + ",0");
					//二维码比对
					requestCompareQRCode(map);
			    }else{
				   map.put("VEHICLEID", null);
				   map.put("commandArgs", "13,8D05," + ObjectNum+"," + Type+"," + EventType + ",1");
				   //二维码比对
				   requestCompareQRCode(map);
				   response.setErrorcode(1);
				   response.setMessage("计时终端不存在");
			    }
			}else{
				map.put("VEHICLEID", null);
				map.put("commandArgs", "13,8D05," + ObjectNum + "," + Type + "," + EventType + ",1");
				//二维码比对
				requestCompareQRCode(map);
				response.setErrorcode(1);
				response.setMessage("终端信息不存在！");
			}
		}
		
		//二维码比对访问外部接口
		public void requestCompareQRCode (Map<String, Object> map) throws Exception{
			String json = new Gson().toJson(map);
			//WebSocketContainer container = ContainerProvider.getWebSocketContainer();
			try {
				if(session != null && session.isOpen()){
					logger.info("=================开始发送消息=======================");
					session.getBasicRemote().sendText(json); // 发送文本消息
					logger.info("=================发送消息=======================");
				}else{
					logger.info("=================链接重新建立=======================");
					session = container.connectToServer(client, new URI(uri));
					session.getBasicRemote().sendText(json); // 发送文本消息
					logger.info("=================再次发送消息=======================");
				}
			} catch (DeploymentException e) {
				logger.info("DeploymentException异常信息:" + e.getMessage());
				session = container.connectToServer(client, new URI(uri));
				session.getBasicRemote().sendText(json);
			} catch (IOException e) {
				logger.info("IOException异常信息:" + e.getMessage());
				session = container.connectToServer(client, new URI(uri));
				session.getBasicRemote().sendText(json);
			} catch (URISyntaxException e) {
				logger.info("URISyntaxException异常信息:" + e.getMessage());
				session = container.connectToServer(client, new URI(uri));
				session.getBasicRemote().sendText(json);
			}
		}
}*/